package com.lovo.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lovo.bean.UserDO;
import com.lovo.bean.dto.UserDTO;
import com.lovo.dao.UserDAO;
import com.lovo.dao.UserDAOImpl;
import com.lovo.service.UserService;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.Optional;

public class UserServiceImpl implements UserService {
    private UserDAO userDAO = new UserDAOImpl();
    @Override
    public UserDTO login(String account, String pwd) {
        UserDO userDO = Optional.ofNullable(userDAO.getByUsernameAndPwd(account,pwd)).orElse(new UserDO());
        UserDTO userDTO = new UserDTO();
        try {
            BeanUtils.copyProperties(userDTO,userDO);
        } catch (IllegalAccessException e) {
        } catch (InvocationTargetException e) {
        }
        return userDTO;
    }

    public static void main(String[] args) throws JsonProcessingException {
        UserService userService = new UserServiceImpl();
        ObjectMapper mapper = new ObjectMapper();
        System.out.println(mapper.writeValueAsString(userService.login("admin","123456")));
    }
}
